import axios from 'axios';
import { Message } from 'iview';
export function fetch (options) {
  return new Promise((resolve, reject) => {
    const instance = axios.create({ //  instance创建一个axios实例，可以自定义配置，可在 axios文档中查看详情
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      timeout: 30 * 100
    });
    //  axios请求拦截
    instance.interceptors.request.use((res) => {
      // console.log(res);
      // let token = '?login=success';
      // if (token) {
      //   res.headers.Authorization = token;
      // }
      // let url = window.location.search;
      // let reg = /(\?login=success)/g;
      // if (!reg.test(url)) {
      //   window.location.href = `http://139.129.231.127:7088/cas/login`;
      //   // setTimeout(() => {
      //   //   // window.location.href = `http://118.190.33.123:7070/dashboard`;
      //   // }, 500);
      // }
      return res;
    }, (error) => {
      return reject(error);
    });
    instance.interceptors.response.use((res) => {
      if (res.status !== 200) {
        Message.error({
          content: '请求失败',
          duration: 5
        });
      }
      return res;
    }, (error) => {
      Message.error({
        content: error,
        duration: 5
      });
      return reject(error);
    });
    instance(options).then((res) => {
      resolve(res);
    }).catch((error) => {
      console.log(`请求异常信息:${error}`);
      reject(error);
    });
  });
}
